<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>zabbix监控nginx状态 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>zabbix监控nginx状态</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Zabbix/">Zabbix</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，在nginx的配置文件中添加如下一段代码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">server </div><div class="line">&#123; </div><div class="line">    listen localhost:80; </div><div class="line">    server_name localhost;</div><div class="line">    location /nginxstatus &#123; </div><div class="line">    stub_status on; </div><div class="line">    access_log off; </div><div class="line">    allow 127.0.0.1;</div><div class="line">    allow 192.168.1.165; </div><div class="line">    deny all; </div><div class="line">    &#125; </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存之后重启nginx</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在zabbix脚本目录下创建脚本监控zabbix状态</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cat nginx_status.sh</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash </span></div><div class="line"><span class="comment"># Script to fetch nginx statuses for tribily monitoring systems </span></div><div class="line"><span class="comment"># Author: admin@huxianglin.cn </span></div><div class="line"><span class="comment"># License: GPLv2 </span></div><div class="line"><span class="comment"># Set Variables </span></div><div class="line"><span class="comment"># Functions to return nginx stats </span></div><div class="line"><span class="keyword">function</span> active &#123;</div><div class="line">curl -s <span class="string">"http://localhost/nginx_status"</span>| awk <span class="string">'/Active/&#123;print $3&#125;'</span></div><div class="line">&#125;</div><div class="line"><span class="keyword">function</span> reading &#123;</div><div class="line">curl -s <span class="string">"http://localhost/nginx_status"</span>| awk <span class="string">'/Reading/&#123;print $2&#125;'</span></div><div class="line">&#125;</div><div class="line"><span class="keyword">function</span> writing &#123;</div><div class="line">curl -s <span class="string">"http://localhost/nginx_status"</span>| awk <span class="string">'/Writing/&#123;print $4&#125;'</span></div><div class="line">&#125;</div><div class="line"><span class="keyword">function</span> waiting &#123;</div><div class="line">curl -s <span class="string">"http://localhost/nginx_status"</span>| awk <span class="string">'/Waiting/&#123;print $6&#125;'</span></div><div class="line">&#125;</div><div class="line"><span class="keyword">function</span> accepts &#123;</div><div class="line">curl -s <span class="string">"http://localhost/nginx_status"</span>| awk NR==3| awk <span class="string">'&#123;print $1&#125;'</span></div><div class="line">&#125;</div><div class="line"><span class="keyword">function</span> handled &#123;</div><div class="line">curl -s <span class="string">"http://localhost/nginx_status"</span>| awk NR==3| awk <span class="string">'&#123;print $2&#125;'</span></div><div class="line">&#125;</div><div class="line"><span class="keyword">function</span> requests &#123;</div><div class="line">curl -s <span class="string">"http://localhost/nginx_status"</span>| awk NR==3| awk <span class="string">'&#123;print $3&#125;'</span></div><div class="line">&#125;</div><div class="line"><span class="comment"># Run the requested function </span></div><div class="line"><span class="keyword">case</span> <span class="string">"<span class="variable">$1</span>"</span> <span class="keyword">in</span></div><div class="line">active)</div><div class="line">active</div><div class="line">;;</div><div class="line">reading)</div><div class="line">reading</div><div class="line">;;</div><div class="line">writing)</div><div class="line">writing</div><div class="line">;;</div><div class="line">waiting)</div><div class="line">waiting</div><div class="line">;;</div><div class="line">accepts)</div><div class="line">accepts</div><div class="line">;;</div><div class="line">handled)</div><div class="line">handled</div><div class="line">;;</div><div class="line">requests)</div><div class="line">requests</div><div class="line">;;</div><div class="line">*)</div><div class="line"><span class="built_in">echo</span> <span class="string">"Usage: <span class="variable">$0</span> &#123;nginx_site_dicovery&#125;"</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"Usage: <span class="variable">$0</span> &#123;active [host] | reading [host] | writing [host] | waiting [host] | accepts [host] | handled [host] | requests [host]&#125;"</span></div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后修改该文件的属主和属组为zabbix，并且具有执行权限</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">chown zabbix.zabbix /usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/nginx_status.sh</div><div class="line">chmod 755 /usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/nginx_status.sh</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后在zabbix_agentd.conf配置文件中添加如下代码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#monitor nginx</span></div><div class="line">UserParameter=nginx.accepts,/usr/<span class="built_in">local</span>/etc/nginx_status.sh accepts</div><div class="line">UserParameter=nginx.handled,/usr/<span class="built_in">local</span>/etc/nginx_status.sh handled</div><div class="line">UserParameter=nginx.requests,/usr/<span class="built_in">local</span>/etc/nginx_status.sh requests</div><div class="line">UserParameter=nginx.connections.active,/usr/<span class="built_in">local</span>/etc/nginx_status.sh active</div><div class="line">UserParameter=nginx.connections.reading,/usr/<span class="built_in">local</span>/etc/nginx_status.sh reading</div><div class="line">UserParameter=nginx.connections.writing,/usr/<span class="built_in">local</span>/etc/nginx_status.sh writing</div><div class="line">UserParameter=nginx.connections.waiting,/usr/<span class="built_in">local</span>/etc/nginx_status.sh waiting</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这段代码可以视自己目录情况而改变</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建好了之后就可以在web页面配置item监控项了</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Zabbix/15. zabbix监控nginx状态/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
